home *** CD-ROM | disk | FTP | other *** search
/ The Complete Utilities To…ka 501 Killer Utilities! / 501 Killer Utilities! (Macworld July 1995).cdr / Programming / OutOfPhase1.1 Source / OutOfPhase Folder / FilterSecondOrderZero.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-03  |  4.3 KB  |  148 lines  |  [TEXT/KAHL]

  1. /* FilterSecondOrderZero.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. /* Based on material from pages 184-190 of */
  26. /* Dodge, Charles and Jerse, Thomas A. */
  27. /* Computer Music:  Synthesis, Composition, and Performance */
  28. /* Schirmer Books, New York, 1985 */
  29.  
  30. #include "MiscInfo.h"
  31. #include "Audit.h"
  32. #include "Debug.h"
  33. #include "Definitions.h"
  34.  
  35. #include "FilterSecondOrderZero.h"
  36. #include "Memory.h"
  37. #include "FloatingPoint.h"
  38.  
  39.  
  40. struct SecondOrderZeroRec
  41.     {
  42.         /* link */
  43.         SecondOrderZeroRec*            Next;
  44.  
  45.         /* state variables */
  46.         float                                        Xm1;
  47.         float                                        Xm2;
  48.  
  49.         /* coefficients */
  50.         float                                        A0;
  51.         float                                        A1;
  52.         float                                        A2;
  53.     };
  54.  
  55.  
  56. static SecondOrderZeroRec*        FreeList = NIL;
  57.  
  58.  
  59. /* flush free list */
  60. void                                            FlushCachedSecondOrderZeroStuff(void)
  61.     {
  62.         while (FreeList != NIL)
  63.             {
  64.                 SecondOrderZeroRec*        Temp;
  65.  
  66.                 Temp = FreeList;
  67.                 FreeList = FreeList->Next;
  68.                 ReleasePtr((char*)Temp);
  69.             }
  70.     }
  71.  
  72.  
  73. /* create a new filter record */
  74. SecondOrderZeroRec*            NewSecondOrderZero(void)
  75.     {
  76.         SecondOrderZeroRec*        Filter;
  77.  
  78.         if (FreeList != NIL)
  79.             {
  80.                 Filter = FreeList;
  81.                 FreeList = FreeList->Next;
  82.             }
  83.          else
  84.             {
  85.                 Filter = (SecondOrderZeroRec*)AllocPtrCanFail(sizeof(SecondOrderZeroRec),
  86.                     "SecondOrderZeroRec");
  87.                 if (Filter == NIL)
  88.                     {
  89.                         return NIL;
  90.                     }
  91.             }
  92.         Filter->Xm1 = 0;
  93.         Filter->Xm2 = 0;
  94.         return Filter;
  95.     }
  96.  
  97.  
  98. /* dispose filter record */
  99. void                                            DisposeSecondOrderZero(SecondOrderZeroRec* Filter)
  100.     {
  101.         CheckPtrExistence(Filter);
  102.         Filter->Next = FreeList;
  103.         FreeList = Filter;
  104.     }
  105.  
  106.  
  107. /* adjust filter coefficients */
  108. void                                            SetSecondOrderZeroCoefficients(SecondOrderZeroRec* Filter,
  109.                                                         float Cutoff, float Bandwidth, FilterScalings Scaling,
  110.                                                         long SamplingRate)
  111.     {
  112.         float                                        C2;
  113.         float                                        C1;
  114.         float                                        D;
  115.  
  116.         CheckPtrExistence(Filter);
  117.         C2 = DEXP(-6.28318530717958648 * Bandwidth / SamplingRate);
  118.         C1 = (-4*C2/(1 + C2)) * DCOS(6.28318530717958648 * Cutoff / SamplingRate);
  119.         switch (Scaling)
  120.             {
  121.                 default:
  122.                     EXECUTE(PRERR(ForceAbort,"SetSecondOrderZeroCoefficients:  unknown scaling"));
  123.                     break;
  124.                 case eFilterDefaultScaling:
  125.                     D = 1;
  126.                     break;
  127.                 case eFilterZeroGain1:
  128.                     D = 1 + C1 + C2;
  129.                     break;
  130.             }
  131.         Filter->A0 = 1 / D;
  132.         Filter->A1 = C1 / D;
  133.         Filter->A2 = C2 / D;
  134.     }
  135.  
  136.  
  137. /* apply filter to a sample value */
  138. float                                            ApplySecondOrderZero(SecondOrderZeroRec* Filter, float Xin)
  139.     {
  140.         float                                        Y;
  141.  
  142.         CheckPtrExistence(Filter);
  143.         Y = Filter->A0 * Xin + Filter->A1 * Filter->Xm1 + Filter->A2 * Filter->Xm2;
  144.         Filter->Xm2 = Filter->Xm1;
  145.         Filter->Xm1 = Xin;
  146.         return Y;
  147.     }
  148.